using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using AutoMapper;
using Core;
using IglooCoder.Commons.WcfNhibernate;
using Infrastucture;
using WcfService1.DTO;

namespace WcfService1
{
    // NOTE: You can use the "Rename" command on the "Refactor" DeliveryType to change the class name "DeliveryTypeServices" in code, svc and config file together.
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    [NHibernateContext]
    public class DeliveryTypeServices : IDeliveryTypeServices
    {
        private readonly DeliveryTypeRepository DeliveryTypeRepository;

        public DeliveryTypeServices()
        {
            DeliveryTypeRepository = new DeliveryTypeRepository(new WcfSessionStorage());
        }

        #region IDeliveryTypeServices Members

        public List<DeliveryTypeDTO> GetList(int page, out long recordCount, string sortProperty = null,
                                             string sortType = "ASC")
        {
            var DeliveryTypes = new List<DeliveryType>();
            var Results = new List<DeliveryTypeDTO>();

            if (string.IsNullOrEmpty(sortProperty))
            {
                DeliveryTypes = DeliveryTypeRepository.GetDataPaged(page, 30).ToList();
            }
            else
            {
                DeliveryTypes = DeliveryTypeRepository.GetDataPaged(page, 30, sortProperty, sortType).ToList();
            }

            recordCount = DeliveryTypeRepository.RecordCount;

            DeliveryTypes.ForEach(x =>
                                      {
                                          var DeliveryTypeDto = new DeliveryTypeDTO();
                                          Mapper.Map(x, DeliveryTypeDto);
                                          //AutoMapper.Mapper.Map(x.ParentDeliveryType, DeliveryTypeDto.ParentDeliveryType);
                                          Results.Add(DeliveryTypeDto);
                                      }
                );

            return Results;
        }

        public bool Save(DeliveryTypeDTO DeliveryTypeDTO)
        {
            bool res;
            try
            {
                DeliveryType DeliveryType =
                    DeliveryTypeRepository.GetDataOne(x => x.DeliveryCode == DeliveryTypeDTO.DeliveryCode);

                if (DeliveryType == null)
                {
                    DeliveryType = new DeliveryType();
                }
                Mapper.Map(DeliveryTypeDTO, DeliveryType);
                DeliveryTypeRepository.SaveOrUpdate(DeliveryType);
                res = true;
            }
            catch (Exception)
            {
                res = false;
            }

            return res;
        }


        public bool Delete(DeliveryTypeDTO DeliveryTypeDto)
        {
            bool res;
            try
            {
                var DeliveryType = new DeliveryType();
                Mapper.Map(DeliveryTypeDto, DeliveryType);
                DeliveryTypeRepository.Delete(DeliveryType);
                res = true;
            }
            catch (Exception)
            {
                res = false;
            }

            return res;
        }


        public List<DeliveryTypeDTO> GetListByCriteria(Dictionary<String, Object> criteria, string expression, int page,
                                                       out long recordCount)
        {
            var DeliveryTypes = new List<DeliveryType>();
            var Results = new List<DeliveryTypeDTO>();
            DeliveryTypes = DeliveryTypeRepository.GetDataPaged(criteria, expression, page, 30).ToList();
            recordCount = DeliveryTypeRepository.RecordCount;
            DeliveryTypes.ForEach(x =>
                                      {
                                          var DeliveryTypeDto = new DeliveryTypeDTO();
                                          Mapper.Map(x, DeliveryTypeDto);
                                          Results.Add(DeliveryTypeDto);
                                      }
                );

            return Results;
        }


        public List<DeliveryTypeDTO> GetAllDeliveryType()
        {
            List<DeliveryType> DeliveryTypes = DeliveryTypeRepository.FetchAll().ToList();
            var Results = new List<DeliveryTypeDTO>();
            DeliveryTypes.ForEach(x =>
                                      {
                                          var DeliveryTypeDto = new DeliveryTypeDTO();
                                          Mapper.Map(x, DeliveryTypeDto);
                                          Results.Add(DeliveryTypeDto);
                                      }
                );

            return Results;
        }


        public DeliveryTypeDTO GetDeliveryType(String kodeDeliveryType)
        {
            DeliveryType DeliveryType = DeliveryTypeRepository.GetDataOne(x => x.DeliveryCode == kodeDeliveryType);
            var DeliveryTypeDto = new DeliveryTypeDTO();
            Mapper.Map(DeliveryType, DeliveryTypeDto);
            return DeliveryTypeDto;
        }

        public List<DeliveryTypeDTO> GetDeliveryTypeList(string kodeDeliveryType)
        {
            List<DeliveryType> DeliveryTypes =
                DeliveryTypeRepository.GetDataList(x => x.DeliveryCode.Contains(kodeDeliveryType)).ToList();
            var Results = new List<DeliveryTypeDTO>();
            DeliveryTypes.ForEach(x =>
                                      {
                                          var DeliveryTypeDto = new DeliveryTypeDTO();
                                          Mapper.Map(x, DeliveryTypeDto);
                                          Results.Add(DeliveryTypeDto);
                                      }
                );

            return Results;
        }

        #endregion
    }
}